//
//  EditViewController.m
//  Delicious
//
//  Created by lanou on 16/5/11.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "EditViewController.h"
#import "WaterFlowLayout.h"
#import "CollectionViewCell.h"
#import "Collection.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "EditDetailViewController.h"

@interface EditViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, WaterFlowLayoutDelegate>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *editArr; // 来存放edit的model

@end

@implementation EditViewController
- (NSMutableArray *)editArr{
    if (!_editArr) {
        _editArr = [NSMutableArray array];
    }
    return _editArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = self.model.name;
    WaterFlowLayout *waterLayout = [[WaterFlowLayout alloc]init];
    // 设置间隔
    waterLayout.itemSpacing = 10;
    // 设置分区偏移
    waterLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 设置列数
    waterLayout.numberOfColumns = 2;
    // 设定itemSize
    CGFloat width =  ([UIScreen mainScreen].bounds.size.width - waterLayout.sectionInset.left - waterLayout.sectionInset.right - (waterLayout.numberOfColumns - 1) * waterLayout.itemSpacing) / waterLayout.numberOfColumns;
    waterLayout.itemSize = CGSizeMake(width, 0);
    // 设置代理
    waterLayout.delegate = self;

    _collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:waterLayout];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [self getData];
    __block EditViewController *vc = self;
[self.collection addFooterWithCallback:^{
    [vc getMoredata];
}];

}

- (void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    NSString *str = [kEdit stringByAppendingString:[NSString stringWithFormat:@"%@&pageNum=1&pageSize=20", self.model.ID]];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        for (NSDictionary *d in arr) {
            Collection *model = [[Collection alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.editArr addObject:model];
        }
        [self.collection reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

//刷新获得更多数据
- (void)getMoredata{
    int number = 1;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *str = [kEditMore stringByAppendingString:[NSString stringWithFormat:@"%d&pageSize=20",++number]];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.collection footerEndRefreshing];
        NSArray *arr = responseObject[@"data"];
        for (NSDictionary *d in arr) {
            Collection *model = [[Collection alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.editArr addObject:model];
        }
        [self.collection reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
    
    
}

// WaterFlowLayout 代理
- (CGFloat)waterFlowLayout:(WaterFlowLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    CGFloat height = 0;
    //    height = arc4random() % 270 + 172;
    return 200;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.editArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.model = self.editArr[indexPath.row];
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Collection *model = self.editArr[indexPath.row];
    EditDetailViewController *vc = [[EditDetailViewController alloc]init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
